/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */

package org.apache.poi.xssf.eventusermodel;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import net.sourceforge.stripes.action.Message;
import afcp.alumni.file.ExcelFile;
import afcp.alumni.file.ExcelFileUploadService;
import afcp.alumni.model.db.FileColumn;
import afcp.alumni.model.db.MemberUtils;

/**
 * A rudimentary XLSX -> CSV processor modeled on the POI sample program
 * XLS2CSVmra by Nick Burch from the package
 * org.apache.poi.hssf.eventusermodel.examples. Unlike the HSSF version, this
 * one completely ignores missing rows.
 * <p/>
 * Data sheets are read using a SAX parser to keep the memory footprint
 * relatively small, so this should be able to read enormous workbooks. The
 * styles table and the shared-string table must be kept in memory. The standard
 * POI styles table class is used, but a custom (read-only) class is used for
 * the shared string table because the standard POI SharedStringsTable grows
 * very quickly with the number of unique strings.
 * <p/>
 * Thanks to Eric Smith for a patch that fixes a problem triggered by cells with
 * multiple "t" elements, which is how Excel represents different formats (e.g.,
 * one word plain and one word bold).
 * 
 * @author Chris Lott
 */
public class XLSX2CSV {

	public static void main(String[] args) throws Exception {
		String directory = "D:";
//		PrintStream writetoEngineer = new PrintStream(new FileOutputStream(
//				directory + "/afcp_members_test.csv", false));
////		PrintStream writetoEngineer = System.out;
//		try (PrintStream outStream = writetoEngineer) {
			args = new String[2];
//			args[0] = directory + "/(AFCP校友录)会员名单V20140511.xlsx";
			args[0] = directory + "/peu de membres.xlsx";
			args[1] = "5";

			if (args.length < 1) {
				System.err.println("Use:");
				System.err.println("  XLSX2CSV <xlsx file> [min columns]");
				return;
			}

			File xlsxFile = new File(args[0]);
			if (!xlsxFile.exists()) {
				System.err.println("Not found or not a file: "
						+ xlsxFile.getPath());
				return;
			}

//			int minColumns = -1;
//			if (args.length >= 2)
//				minColumns = Integer.parseInt(args[1]);

			// The package open is instantaneous, as it should be.
//			OPCPackage p = OPCPackage.open(xlsxFile.getPath(),
//					PackageAccess.READ);
			
			ExcelFile file = new ExcelFile();
//			file.setHeaders(new HashMap<String, String>());
//			Map<String, String> headers = new HashMap<>();
//			for (FileColumn column : MemberUtils.DEFAULT_COLUMNS) {
//				headers.put(column.getId(), column.getFileColumnName());
//			}
//			file.setHeaders(headers);
			file.setHeaders(new ArrayList<FileColumn>(MemberUtils.defaultColumns()));
			file.setSheetNumber(1);
			List<Message> failures = new LinkedList<Message>();
			
			new ExcelFileUploadService().handle(new FileInputStream(xlsxFile), file, failures);
//			XLSX2CSV xlsx2csv = new XLSX2CSV(p, outStream, minColumns, file);
//			xlsx2csv.process();
//		}
	}

}